<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="tableText['personnelName']">
          <el-input v-model="searchForm.staffName" clearable :placeholder="$t('common.input')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['accountState']">
          <el-select v-model="searchForm.status" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in tableText['accountStatus']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="id"
          label="ID"
          min-width="80"
        />
        <el-table-column
          fixed="left"
          prop="staffName"
          :label="tableText['personnelName']"
          min-width="120"
        />
        <el-table-column
          prop="phonenumber"
          :label="tableText['phoneNumber']"
          min-width="120"
        />
        <el-table-column
          prop="groupId"
          :label="tableText['group']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setGroupName(scope.row['groupId']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="stageId"
          :label="tableText['stage']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setStage(scope.row['stageId']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firmName"
          :label="tableText['company']"
          min-width="120"
        />
        <el-table-column
          prop="firmType"
          :label="tableText['companyType']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setFirmType(scope.row['firmType']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="authority"
          :label="tableText['authority']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setAuthority(scope.row['authority']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="workStatus"
          :label="tableText['workState']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setState(scope.row['workStatus']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.workStatus.toString() === '1'" size="small" @click="updateItem(scope.row, 2)">{{ $t('administrator.memberManagement.pause') }}</el-button>
            <el-button v-if="scope.row.workStatus.toString() === '2'" size="small" @click="updateItem(scope.row, 1)">{{ $t('administrator.memberManagement.open') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { getAllCreditReviewGroup, getReview, getCurrentStaffId, queryCreditReviewStaff, updateCreditReviewStaff } from '@/api/risk'
export default {
  name: 'MemberManagement',
  data() {
    return {
      loading: true,
      tableText: {},
      totalNumber: 0,
      allGroup: [],
      allStage: [],
      tableData: [],
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('administrator.memberManagement')
    this.getAllGroup()
    this.getStageData()
    setTimeout(() => {
      this.getStaffId()
    }, 10)
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        staffName: null,
        status: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getStaffId()
    },
    reset() {
      this.searchForm = this.params()
      this.getStaffId()
    },
    getStaffId() {
      this.loading = true
      getCurrentStaffId()
        .then(res => {
          if (res.success) {
            if (res.data && res.data['groupId'] !== null) {
              this.getData()
            } else {
              this.tableData = []
              this.loading = false
            }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.tableData = []
          this.loading = false
          this.$message.error(err.message)
        })
    },
    getData() {
      queryCreditReviewStaff(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    updateItem(item, state) {
      this.loading = true
      item['workStatus'] = state
      updateCreditReviewStaff(item)
        .then(res => {
          if (res.success) {
            this.$message.success(this.$t('common.tip.editSuccess'))
            this.getStaffId()
          } else {
            this.loading = false
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setGroupName(data) {
      const name = this.allGroup.filter(item => {
        return item.id === data
      })
      return (name && name[0] && name[0].groupName) ? name[0].groupName : ''
    },
    setStage(data) {
      const name = this.allStage.filter(item => {
        return item.id === data
      })
      return (name && name[0] && name[0].flowName) ? name[0].flowName : ''
    },
    setFirmType(firmType) {
      if (firmType !== null) {
        const name = this.$t('letterReviewMember.companyType').filter(item => {
          return item.value === firmType
        })
        return (name.length > 0 && name[0].desc) ? name[0].desc : ''
      }
    },
    setState(data) {
      if (data !== null) {
        const name = this.tableText['workStatus'].filter(item => {
          return item.value + '' === data
        })
        return (name.length > 0 && name[0].desc) ? name[0].desc : ''
      }
    },
    setAuthority(authority) {
      if (authority !== null) {
        const arr = []
        authority.split('_').forEach(v => {
          const name = this.$t('letterReviewMember.permissions').filter(item => {
            return item.value === v
          })
          if (name.length > 0 && name[0].desc) {
            arr.push(name[0].desc)
          }
        })
        return arr.length > 0 ? arr.join(', ') : ''
      }
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getStageData() {
      const data = {
        type: 1
      }
      getReview(data)
        .then(res => {
          if (res.success) {
            this.allStage = res.data.filter(item => {
              return item['status'] === 1
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getAllGroup() {
      getAllCreditReviewGroup()
        .then(res => {
          if (res.success) {
            this.allGroup = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
